"""
解析文件中的id和图片文件地址
"""
import json

import facade
from parsel import Selector
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseUrl import BaseUrl

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "ebmbook_mirrorbeihua", "download", "html")
configfile = BaseDir.get_new_path(curpath, "db.ini")


class ParaHtml(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)

    def para_files(self):
        for file in BaseDir.get_dir_all_files(dirPath):
            Listvalues = []
            strings = BaseDir.single_read_file(file)
            dicts = json.loads(strings)
            downdate = dicts["downdate"]
            html = dicts["html"]
            select = Selector(text=html)
            divlist = select.xpath(
                '//*[@id="maincontent wrap_content"]/div[@class="grid_booklist_1 wrap_content"]/div[@class="navigation_big_1"]/div[@class="productlist"]/div[@class="productbox3"]')
            for div in divlist:
                coverurl = div.xpath('.//*[@id="Img1"]/@src').get()
                url = div.xpath('.//div[@class="productinfo_1"]/div[@class="product_name_1"]/a/@href').get()
                print(url)
                a = div.xpath('.//div[@class="productinfo_1"]/div[@class="product_name_1"]/a')
                title = a.xpath('string(.)').get().replace("Titles:", "").strip()  # extract()[0].strip()
                print(title)
                divlist = div.xpath(
                    './/div[@class="productinfo_1"]/div[@class="productdescription_1"]/div[@class="tags"]')
                for div in divlist:
                    stringmsg = ("".join(div.xpath(".//text()").getall())).strip()
                    print(stringmsg)
                    if stringmsg.find("Author") > -1:
                        author = stringmsg.replace("Author:", "")
                    elif stringmsg.find("ISBN") > -1:
                        isbn = stringmsg.replace("ISBN:", "")
                    elif stringmsg.find("Press") > -1:
                        Press = stringmsg.replace("Press:", "")
                    elif stringmsg.find("Formats") > -1:
                        Formats = stringmsg.replace("Formats:", "")

                rawid = BaseUrl.urlQuery2dic(url)["id"][0]
                print(rawid)
                Listvalues.append((rawid, title, url, author, isbn, Press, downdate, Formats, coverurl))
            sql = "insert ignore into book (`rawid`,`title`,`url`,`author`,`isbn`,`press`,`downdate`,`formats`,`imageurl`) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
            self.mysqlutils.ExeSqlMany(sql,Listvalues)


if __name__ == "__main__":
    para = ParaHtml()
    para.para_files()
